package com.xuqi.Game.Tetris;

import java.util.ArrayList;
import java.util.Random;

import android.R.integer;
import android.graphics.Color;
import android.util.Log;

import com.xuqi.Game.FairyActionIF;
import com.xuqi.Game.GameDrawElemIF;
import com.xuqi.Game.GameElemState;
import com.xuqi.Game.GameMapData;

import dalvik.system.TemporaryDirectory;

public class Tetris extends GameElemState implements FairyActionIF
{
	
	
	private static final String TAG = "Tetris";
	public Tetris()
	{
		super();
		// TODO Auto-generated constructor stub
		Random random = new Random(System. currentTimeMillis());
		m_Dir = Math.abs(random.nextInt())%4;
		int i = Math.abs(random.nextInt())% m_ColorList.length;
		m_TetrisColor = m_ColorList[i];
		SetCurrentState(m_Dir);
	}

	public int GetColor()
	{
		return m_TetrisColor;
	}
	public void SetStateList(ArrayList<GameDrawElemIF> statelist)
	{
		SetStateArray(statelist);
	}
	@Override
	public boolean MoveLeft()
	{
		// TODO Auto-generated method stub
		SetLocation(--m_X, m_Y);
		return true;
		
	}

	@Override
	public GameDrawElemIF SetLocation(int left, int top)
	{
		// TODO Auto-generated method stub
		m_X = left;
		m_Y = top;
		return super.SetLocation(left, top);
	}

	@Override
	public boolean MoveRight()
	{
		// TODO Auto-generated method stub
		SetLocation(++m_X, m_Y);
		return true;
	}

	@Override
	public boolean MoveDown()
	{
		// TODO Auto-generated method stub
		SetLocation(m_X, ++m_Y);
		return true;
	}

	@Override
	public boolean Turn()
	{
		// TODO Auto-generated method stub
		m_Dir = (++m_Dir)%4;
		Log.d(TAG, "Turn: " + m_Dir);
		SetCurrentState(m_Dir);
		return true;
		
	}
	
	public byte[][] GetShape(byte [][][] shape,int index)
	{
		byte [][] nShape = new byte[4][4];
		for(int i = 0; i < 4; i++)
		{
			nShape[i] = shape[i][index];
		}
		return nShape;
	}
	
	public byte[][][] ChangeDataShape(byte[][][] m_shape)
	{

		
		int i = m_shape.length;
		int j = i > 0 ? m_shape[0].length : 0;
		int k = j > 0 ? m_shape[0][0].length : 0;
		byte [][][] temp = new byte[i][j][k]; 
		
		for(int z = 0; z < m_shape.length; z++)
		for(int y = 0; y< m_shape[z].length; y++)
		{
			for(int x = 0; x< m_shape[z][y].length; x++)
			{
				if(m_shape[z][y][x] == A)
				{
					temp[z][y][x] = 0;
				}
				else
				{
					temp[z][y][x] = 1;
				}
			}
		}
		return temp;
	}
	
	@Override
	public boolean MoveLeftTest()
	{
		// TODO Auto-generated method stub
		m_NextX = m_X - 1;
		m_NextY = m_Y;
		m_NextDir = m_Dir;
		return Test(m_NextDir,m_NextX,m_NextY);
	}

	@Override
	public boolean MoveRightTest()
	{
		// TODO Auto-generated method stub
		m_NextX = m_X + 1;
		m_NextY = m_Y;
		m_NextDir = m_Dir;
		return Test(m_NextDir,m_NextX,m_NextY);
	}

	@Override
	public boolean MoveDownTest()
	{
		// TODO Auto-generated method stub
		m_NextX = m_X;
		m_NextY = m_Y + 1;
		m_NextDir = m_Dir;
		return Test(m_NextDir,m_NextX,m_NextY);
	}

	@Override
	public boolean TurnTest()
	{
		// TODO Auto-generated method stub
		m_NextX = m_X;
		m_NextY = m_Y;
		m_NextDir = m_Dir;
		m_NextDir =(++m_NextDir)%4;;
		return Test(m_NextDir,m_NextX,m_NextY);
	}
	@Override
	public boolean Save()
	{
		GameMapData.Instance().AddShape(GetShape(m_DataShape,m_Dir), m_X, m_Y);
		return true;
	}
	
	public boolean Test(int NextDir,int NextX,int NextY)
	{
		boolean bRet = !GameMapData.CheckOver(GetShape(m_DataShape,NextDir), NextX, NextY);
		bRet = bRet && !GameMapData.CheckClick(GetShape(m_DataShape,NextDir), NextX, NextY);
		return bRet;
	}
	
	protected final byte A = -1;
	protected final byte B = 0;
	protected int m_NextX = 0;
	protected int m_NextY = 0;
	protected int m_NextDir = 0;
	
	protected int m_X = 0;
	protected int m_Y = 0;
	protected int m_Dir = 0;
	protected int m_TetrisColor = Color.WHITE;
	protected byte[][][] m_DataShape = null;
	private int m_ColorList[] = new int[]{
			Color.RED,
			Color.BLUE,
			Color.GREEN,
			Color.RED,
			Color.WHITE,
			Color.YELLOW,
	};

	/*
	 * 
L
{{B,A,A,A},{B,B,B,A},{B,B,A,A},{A,A,B,A,}},															
{{B,A,A,A},{B,A,A,A},{A,B,A,A},{B,B,B,A,}},															
{{B,B,A,A},{A,A,A,A},{A,B,A,A},{A,A,A,A,}},															
{{A,A,A,A},{A,A,A,A},{A,A,A,A},{A,A,A,A,}},															
I															
{{A,A,A,A},{B,A,A,A},{A,A,A,A},{B,A,A,A,}},															
{{B,B,B,B},{B,A,A,A},{B,B,B,B},{B,A,A,A,}},															
{{A,A,A,A},{B,A,A,A},{A,A,A,A},{B,A,A,A,}},															
{{A,A,A,A},{B,A,A,A},{A,A,A,A},{B,A,A,A,}},															
S															
{{A,B,B,A},{B,A,A,A},{A,B,B,A},{B,A,A,A,}},															
{{B,B,A,A},{B,B,A,A},{B,B,A,A},{B,B,A,A,}},															
{{A,A,A,A},{A,B,A,A},{A,A,A,A},{A,B,A,A,}},															
{{A,A,A,A},{A,A,A,A},{A,A,A,A},{A,A,A,A,}},															
Z															
{{B,B,A,A},{A,B,A,A},{B,B,A,A},{A,B,A,A,}},															
{{A,B,B,A},{B,B,A,A},{A,B,B,A},{B,B,A,A,}},															
{{A,A,A,A},{B,A,A,A},{A,A,A,A},{B,A,A,A,}},															
{{A,A,A,A},{A,A,A,A},{A,A,A,A},{A,A,A,A,}},															
J															
{{A,B,A,A},{B,A,A,A},{B,B,A,A},{B,B,B,A,}},															
{{A,B,A,A},{B,B,B,A},{B,A,A,A},{A,A,B,A,}},															
{{B,B,A,A},{A,A,A,A},{B,A,A,A},{A,A,A,A,}},															
{{A,A,A,A},{A,A,A,A},{A,A,A,A},{A,A,A,A,}},															
O															
{{B,B,A,A},{B,B,A,A},{B,B,A,A},{B,B,A,A,}},															
{{B,B,A,A},{B,B,A,A},{B,B,A,A},{B,B,A,A,}},															
{{A,A,A,A},{A,A,A,A},{A,A,A,A},{A,A,A,A,}},															
{{A,A,A,A},{A,A,A,A},{A,A,A,A},{A,A,A,A,}},															
T															
{{A,B,A,A},{A,B,A,A},{B,B,B,A},{A,B,A,A,}},															
{{B,B,B,A},{B,B,A,A},{A,B,A,A},{A,B,B,A,}},															
{{A,A,A,A},{A,B,A,A},{A,A,A,A},{A,B,A,A,}},															
{{A,A,A,A},{A,A,A,A},{A,A,A,A},{A,A,A,A,}},		
	 */
}
